package com.computergames.server.service.Implement;

import com.computergames.server.domain.ClientState;
import com.computergames.server.message.AcknowledgeMessage;
import com.computergames.server.message.ReadyResponseMessage;
import com.computergames.server.message.cardMessage.DealMessage;
import com.computergames.server.server.session.RoomSessionFactory;
import com.computergames.server.service.Interface.CheckAndUpdateClientState;
import com.computergames.server.service.Interface.HandleAcknowledgeMessage;
import com.computergames.server.util.SpringUtil;
import io.netty.channel.Channel;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class HandleAcknowledgeMessageImpl implements HandleAcknowledgeMessage {
    private static final ConcurrentHashMap<String, AtomicInteger> roomDealCounts = new ConcurrentHashMap<>();
    private static final ConcurrentHashMap<String, AtomicInteger> roomBidCounts = new ConcurrentHashMap<>();
    private CheckAndUpdateClientState checkAndUpdateClientState;
    @Override
    public void handleAcknowledgeMessage(String roomName, AcknowledgeMessage msg) {
        String acknowledgeContent = msg.getAcknowledge();
        List<Channel> channels = RoomSessionFactory.getRoomSession().getMembersChannel(roomName);
        String[] parts = acknowledgeContent.split("\\s+");

        if (parts.length < 2) {
            // Handle invalid acknowledge message
            System.err.println("Invalid acknowledge message format");
            return;
        }

        String status = parts[0];
        String action = parts[1];

        if ("OK".equals(status)) {
            switch (action) {
                case "DEAL":
                    handleDealAcknowledge(channels, roomName);
                    break;
//                case "BID":
//                    handleBidAcknowledge(channels, roomName);
//                    break;
                default:
                    // Handle unknown action
                    System.err.println("Unknown action in acknowledge message: " + action);
                    break;
            }
        } else {
            // Handle non-OK status
            System.err.println("Non-OK status in acknowledge message: " + status);
        }
    }

    private void handleDealAcknowledge(List<Channel> channels, String roomName) {
        checkAndUpdateClientState = SpringUtil.getBean(CheckAndUpdateClientState.class);
        AtomicInteger count = roomDealCounts.computeIfAbsent(roomName, k -> new AtomicInteger(0));
        count.incrementAndGet();
        if (count.get() == channels.size()) {
            channels.forEach(channel -> {
                checkAndUpdateClientState.updateState(channel, ClientState.CARD_DEAL);
                channel.writeAndFlush(new DealMessage(true, "所有人已拿到手牌，请开始叫牌"));
            });
            roomDealCounts.remove(roomName);
        }
    }

//    private void handleBidAcknowledge(List<Channel> channels, String roomName) {
//        AtomicInteger count = roomBidCounts.computeIfAbsent(roomName, k -> new AtomicInteger(0));
//        count.incrementAndGet();
//        if (count.get() == channels.size()) {
//            channels.forEach(channel -> {
//                //checkAndUpdateClientState.updateState(channel, ClientState.GAME_STARTED);
//                channel.writeAndFlush(new ReadyResponseMessage(true, "所有人已进行叫牌"));
//            });
//        }
//        roomBidCounts.remove(roomName);
//    }
}
